package com.toune.dltools

import android.content.Context
import android.view.*
import com.qmuiteam.qmui.widget.dialog.QMUIDialog
import kotlin.properties.Delegates

open abstract class DLBaseDialog : QMUIDialog {

    private var layout by Delegates.notNull<Int>()

    constructor(context: Context?, layoutId: Int) : super(context) {
        this.layout = layoutId
        initView()
    }

    constructor(context: Context?, layoutId: Int, themeResId: Int) : super(context, themeResId) {
        this.layout = layoutId
        initView()
    }

    protected constructor(
        context: Context?,
        layoutId: Int,
        cancelable: Boolean,
    ) : super(context) {
        this.layout = layoutId
        setCanceledOnTouchOutside(cancelable)
        initView()
    }

    var dialogView: View? = null
    private fun initView() {
        dialogView = LayoutInflater.from(context).inflate(layout, null)
        initEvent()
        setContentView(dialogView!!)
    }

    protected abstract fun initEvent()

    override fun show() {
        super.show()
        /**
         * 设置宽度全屏，要设置在show的后面
         */
        val layoutParams: WindowManager.LayoutParams = window!!.attributes
        layoutParams.gravity = Gravity.CENTER
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT
        window!!.decorView.setPadding(20, 0, 20, 0)
        window!!.attributes = layoutParams
    }
}